﻿/* 12. Extend the program to support also subtraction and multiplication of polynomials. */

using System;

public class SubtractionAndMultiplicationOfPolynomials
{
    public static void Main()
    {
        AddsOfPolynomials.DataInput();
        AddsOfPolynomials.PrintResult(Subtraction(AddsOfPolynomials.a, AddsOfPolynomials.b), "Minus");
        AddsOfPolynomials.PrintResult(Multiplication(AddsOfPolynomials.a, AddsOfPolynomials.b), "Multiply By");
    }

    public static decimal[] Subtraction(decimal[] a, decimal[] b)
    {
        int maxLength = Common.GetMax(a.Length, b.Length);
        decimal[] aMinusB = new decimal[maxLength];

        for (int i = 0; i < maxLength; i++)
            aMinusB[i] = (i < a.Length ? a[i] : 0) - (i < b.Length ? b[i] : 0);

        return aMinusB;
    }

    public static decimal[] Multiplication(decimal[] a, decimal[] b)
    {
        int lengthOfA = a.Length;
        int lengthOfB = b.Length;
        int lengthOfMultiplication = lengthOfA + lengthOfB - 1;
        decimal[] aMultiplyByB = new decimal[lengthOfMultiplication];

        for (int element = 0; element < lengthOfMultiplication; element++)
            aMultiplyByB[element] = 0;

        for (int i = 0; i < lengthOfA; i++)
            for (int j = 0; j < lengthOfB; j++)
                aMultiplyByB[i + j] += a[i] * b[j];

        return aMultiplyByB;
    }
}